package joat.application.ui;

import joat.application.automatons.UIManagerAutomaton;
import joat.application.misc.Constants;
import society.framework.android.SocietyActivity;
import society.framework.core.SocietyInformation;
import society.framework.core.communication.Message;
import society.framework.core.communication.MessageRouter;
import society.framework.core.xml.XMLAutomaton;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends SocietyActivity {
	public static XMLAutomaton newsFetcherAutomaton;

	@Override
	protected void initializeSociety() {
		try {
			//adding the addresses
			SocietyInformation.getRemoteSocietyAddresses().add("172.17.50.102");
			SocietyInformation.getRemoteSocietyAddresses().add("192.168.2.102");
			SocietyInformation.getRemoteSocietyAddresses().add("192.168.1.3");
			SocietyInformation.getRemoteSocietyAddresses().add("192.168.1.120");
			
			
			//UI Manager
			UIManagerAutomaton uiManagerAutomaton = new UIManagerAutomaton(this);
			societyManager.addAutomaton(SocietyInformation.UI_MANAGER_NAME, uiManagerAutomaton);
			MessageRouter.getInstance().subscribe(SocietyInformation.UI_MANAGER_NAME, uiManagerAutomaton);
			
			//Test Automaton
			/*
			testAutomaton = new XMLAutomaton();
			if(testAutomaton.deserializeFromStream(getResources().getAssets().open("test_automaton4.xml"))){
				societyManager.addAutomaton(testAutomaton.getName(), testAutomaton);
			} else {
				Toast.makeText(this, "Nu s-a putut deserializa automatul de test", Toast.LENGTH_LONG).show();
			}
			*/
			
			//News Fetcher Automaton
			newsFetcherAutomaton = new XMLAutomaton();
			if(newsFetcherAutomaton.deserializeFromStream(getResources().getAssets().open("news_fetcher_automaton.xml"))){
				societyManager.addAutomaton(newsFetcherAutomaton.getName(), newsFetcherAutomaton);
			} else {
				Toast.makeText(this, "Automaton could not be deserialized from assets", Toast.LENGTH_LONG).show();
			}
			
			//Start the transfer threads
			MessageRouter.getInstance().startNetworkMessaging();
			societyManager.startAutomataTransfer();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
        MessageRouter.getInstance().send(SocietyInformation.UI_MANAGER_NAME,
        		new Message(Constants.UI_NAME, Constants.SCREEN_ORIENTATION_CHANGED));
        Log.d("JOAT", "onConfigurationChanged()");
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		Object[] result = new Object[4];
		result[0] = Constants.ACTIVITY_RESULT;
		result[1] = requestCode;
		result[2] = resultCode;
		result[3] = data;
		MessageRouter.getInstance().send(SocietyInformation.UI_MANAGER_NAME, new Message(Constants.UI_NAME, result));
		
		super.onActivityResult(requestCode, resultCode, data);
	}

	/* MENU ACTIONS */
	
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main_activity_menu, menu);
        return true;
    }

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menu_item_send:
			MessageRouter.getInstance().send(SocietyInformation.UI_MANAGER_NAME,
					new Message(Constants.UI_NAME, Constants.SEND_AUTOMATON_REQUEST));
			break;
		case R.id.menu_item_request:
			MessageRouter.getInstance().send(SocietyInformation.UI_MANAGER_NAME,
					new Message(Constants.UI_NAME, Constants.REQUEST_AUTOMATON_REQUEST));
			break;
		case R.id.menu_item_auto_scroll:
			item.setChecked(!item.isChecked());
			ListView conversationList = (ListView) findViewById(R.id.conversationList);
			if(item.isChecked()){
				conversationList.setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
			} else {
				conversationList.setTranscriptMode(ListView.TRANSCRIPT_MODE_DISABLED);
			}
			break;
		default:

		}
		return super.onMenuItemSelected(featureId, item);
	}

	
	
	/* DEBUG REASONS */
    
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d("JOAT", "onCreate()");
    }

	@Override
	protected void onPause() {
		super.onPause();
        Log.d("JOAT", "onPause()");
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
        Log.d("JOAT", "onDestroy()");
	}

	@Override
	protected void onRestart() {
		super.onRestart();
        Log.d("JOAT", "onRestart()");
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
        Log.d("JOAT", "onRestoreInstanceState()");
	}

	@Override
	protected void onResume() {
		super.onResume();
        Log.d("JOAT", "onResume()");
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
        Log.d("JOAT", "onSaveInstanceState()");
	}

	@Override
	protected void onStart() {
		super.onStart();
        Log.d("JOAT", "onStart()");
	}

	@Override
	protected void onStop() {
		super.onStop();
        Log.d("JOAT", "onStop()");
	}
	
}
